package com.zhihaoliang.coroutine.buss.home

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.zhihaoliang.coroutine.R
import com.zhihaoliang.coroutine.base.BaseActivity
import com.zhihaoliang.coroutine.databinding.HomeItemBinding
import com.zhihaoliang.coroutine.util.log

/**
 * @author 支豪亮
 * @date 2022/10/20 10:59
 * @des HomeActivity 中 RecyclerView 的 Adapter
 */
class HomeAdapter(private val list: List<HomeBean>, private val itemClick: OnItemClick) :
    RecyclerView.Adapter<HomeViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val dataBinding =  HomeItemBinding.inflate(layoutInflater,parent,false)
        return HomeViewHolder(dataBinding)
    }

    override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
        log("onBindViewHolder")
        holder.dataBinding.homebean = list[position]
        holder.dataBinding.itemClick = itemClick
    }


    override fun getItemCount(): Int = list.size

}

class HomeViewHolder(val dataBinding: HomeItemBinding) : RecyclerView.ViewHolder(dataBinding.root)


fun interface OnItemClick {
    fun onItemClick(homeBean: HomeBean)
}

